﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using XMK.Tadong.Model;
using XMK.Tadong.Biz;
using System.Data;
using System.Configuration;


namespace XMK.Tadong.Web.WebFileExplorer.WebServices
{
    /// <summary>
    /// ShareFile1 的摘要说明
    /// </summary>
    public class ShareFile1 : IHttpHandler
    {
      
        public void ProcessRequest(HttpContext context)
        {
            //string xmkcheck = context.Request.Params["xmkIsSelected"].ToString();//项目库选中信息
            //string teamcheck = context.Request.Params["teamIsSelected"].ToString();//课题组选中信息
            string teamIdList = context.Request.Params["ids"].ToString();//课题组id列表
            string fileId = context.Request.Params["filepath"].ToString();//文件id
            string name = context.Request.Params["filename"].ToString();//文件名
            string userId = context.Request.Cookies["user_id"].Value;
            Tb_Person_File_ShareBiz sharefileBiz = new Tb_Person_File_ShareBiz();
          
            ////共享文件
            TB_PERSON_FILE_SHAREInfo sharefileObj = new TB_PERSON_FILE_SHAREInfo();
            //如果项目库选中，共享文档到共享表
            //if (xmkcheck.Equals("1"))
            //{
            //     string[] fileids = fileId.Split(',');
            //     for (int j = 0; j < fileids.Length; j++)
            //     {
            //         //判断是否已经共享到项目库了,如果没有共享过则共享
            //         if (!sharefileBiz.hava_shared_xmk(fileids[j]))
            //         {
            //             sharefileObj.SHARE_OBJ_ID = 0;//项目库空间默认id
            //             sharefileObj.SHARE_OBJ_TYPE = "20";
            //             sharefileObj.FILE_ID = decimal.Parse(fileids[j]);
            //             sharefileObj.CREATE_TIME = DateTime.Now;
            //             sharefileObj.UPD_TIME = sharefileObj.CREATE_TIME;
            //             sharefileBiz.Share(sharefileObj);
            //         }
            //     }
            //}
            //如果课题组选中，遍历课题组id，将文档路径上传到共享表
            //if (teamcheck.Equals("1"))
            //{
                string[] sArray = teamIdList.Split(';');
                string[] fileids = fileId.Split(',');
                for (int j = 0; j < fileids.Length; j++)
                {
                    for (int i = 0; i < sArray.Length - 1; i++)
                    {
                        if (sArray[i].ToString() == "17")
                        {
                            ///共享到项目库
                            if (!sharefileBiz.hava_shared_xmk(fileids[j]))
                            {
                                sharefileObj.SHARE_OBJ_ID = 17;//项目库空间默认id
                                sharefileObj.SHARE_OBJ_TYPE = "20";
                                sharefileObj.FILE_ID = decimal.Parse(fileids[j]);
                                sharefileObj.CREATE_TIME = DateTime.Now;
                                sharefileObj.UPD_TIME = sharefileObj.CREATE_TIME;
                                sharefileBiz.Share(sharefileObj);
                            }

                        }
                        else
                        {
                            ///共享到课题组
                            if (!sharefileBiz.hava_shared_team(fileids[j], sArray[i].ToString()))
                            {
                                sharefileObj.SHARE_OBJ_ID = decimal.Parse(sArray[i].ToString());//课题组id
                                sharefileObj.SHARE_OBJ_TYPE = "10";
                                sharefileObj.FILE_ID = decimal.Parse(fileids[j]);
                                sharefileObj.CREATE_TIME = DateTime.Now;
                                sharefileObj.UPD_TIME = sharefileObj.CREATE_TIME;
                                sharefileBiz.Share(sharefileObj);
                            }
                        }
                    }
                }
            //}
            //context.Response.ContentType = "application/xml";
            //string result = "yes";
            //context.Response.Write(result);
            //context.Response.End();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}